﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GLImp;
using System.Drawing;
using CastleOfTheDisillusioned.Weapons;
using OpenTK;
using CastleOfTheDisillusioned.Input;
using CastleOfTheDisillusioned.Overworld;
using CastleOfTheDisillusioned.Location.Maps;

namespace CastleOfTheDisillusioned.Location {
	class Player : Entity {
		public double VSpeed = 0;
		public double HSpeed = 0;
		const double Gravity = 0.02f;
		double Speed = .1;
		public Direction Facing;
		public Weapon CurrentWeapon;
		Vector2d WeaponOffset = new Vector2d(0.5, 0.5);

		public bool moving;

		public double HP;
		public double MaxHP;

		bool DoubleJump = true;
		int JumpCount = 0;

		Map CurrentMap {
			get {
				return Location_State.CurrentMap;
			}
		}

		bool OnGround {
			get {
				return !CurrentMap.PlaceFree(this, X, Y + 1);
			}
		}

		bool InAir {
			get {
				return !OnGround;
			}
		}

		bool Attacking {
			get {
				return CurrentWeapon.IsAttacking();
			}
		}

		public Player() {
			X = 20;
			Y = 20;
			Width = 1;
			Height = 2;
			Facing = Direction.Right;
			moving = false;

			HP = 100;
			MaxHP = 100;

			CurrentWeapon = new Whip();
		}

		public override void Draw() {
			GraphicsManager.DrawRectangle(X, Y, Width, Height, Color.Blue);
			GraphicsManager.DrawRectangle(X + (Facing == Direction.Right ? 13 / 16.0 : 0), Y + 3.0 / 16, 3.0 / 16, 3.0 / 16, Color.Cyan);
			if (Attacking) {
				CurrentWeapon.Draw(X + WeaponOffset.X, Y + WeaponOffset.Y, Facing);
			}
		}

		public override void Update() {
			//Horizontal Movement
			if (Controls.IsDown(Control.Left)) {
				if (!Attacking || InAir) {
					Move(-Speed, 0);
				}
				
				if (!Attacking){
					Facing = Direction.Left;
				}
			}

			if (Controls.IsDown(Control.Right)) {
				if (!Attacking || InAir) {
					Move(Speed, 0);
				}
				if (!Attacking){
					Facing = Direction.Right;
				}
			}


			Move(HSpeed, 0);

			if (OnGround) {
				HSpeed *= 0.9f;
				if (Math.Abs(HSpeed) < 0.05) {
					HSpeed = 0;
				}
			}

			//Vertical Movement
			if (Math.Abs(Move(0, VSpeed).Y) < Math.Abs(VSpeed)) {
				VSpeed = 0;
			} else {
				VSpeed += Gravity;
			}

			if (Controls.IsPressed(Control.Jump) && (OnGround || (DoubleJump && JumpCount < 2)) && !Attacking) {
				if (JumpCount == 0 || !DoubleJump) {
					VSpeed = -.55;
				} else {
					VSpeed = -.45;
				}
				JumpCount++;
			} else if (OnGround && DoubleJump) {
				 JumpCount = 0;
			} else if (InAir && DoubleJump && JumpCount < 1) {
				JumpCount++; //We fell of a ledge, which removes a double jump
			}
			if (Controls.IsReleased(Control.Jump) && InAir) {
				if (VSpeed < 0) {
					VSpeed /= 2;
				}
			}


			//Combat
			if (!Attacking && Controls.IsPressed(Control.Attack)) {
				CurrentWeapon.Attack();
			} else {
				CurrentWeapon.Update(X + WeaponOffset.X, Y + WeaponOffset.Y, Facing);
			}

			if (Controls.IsPressed(Control.BackDash) && OnGround && HSpeed == 0 && !Attacking) {
				if (Facing == Direction.Left) {
					HSpeed = .5;
				} else {
					HSpeed = -.5;
				}
			}
		}

		internal void TakeDamage(double Damage) {
			HP -= Damage;
		}

		internal void Throw(double XSpeed, double YSpeed) {
			HSpeed = XSpeed;
			VSpeed = YSpeed;
		}
	}
}
